//
//  ItemThumbnailView.m
//  SAYAKA_Table
//
//  Created by numanuma08 on 2013/07/20.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#import "ItemThumbnailView.h"
#import "ImageUtils.h"
#import "DisplayUtils.h"

@implementation ItemThumbnailView
@synthesize imageView = _imageView;
@synthesize commentLabel = _commentLabel;
@synthesize priceLabel = _priceLabel;
@synthesize nameLabel =_nameLabel;
@synthesize image = _image;
@synthesize item = _item;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

+ (id)itemThumbnailView
{
    UINib *nib = [UINib nibWithNibName:@"ItemThumbnailView" bundle:nil];
    ItemThumbnailView *view = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
    [view initializeLayout];
    return view;
}

- (void)initializeLayout
{
    _myFrame = self.frame;
}

- (void)startAutoScroll
{
    _scrollview.contentOffset = CGPointMake(0, 0);
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollOnTimer:)
                                   userInfo:nil repeats:TRUE];
}

- (void)scrollOnTimer:(NSTimer*)timer
{
    CGPoint point = _scrollview.contentOffset;
    _scrollview.contentOffset = CGPointMake(point.x+1, point.y);
    
    if (_scrollview.contentOffset.x >= _scrollview.contentSize.width-300) {
        [timer invalidate];
    }
}

- (void)setItem:(Item *)item
{
    _priceLabel.text = [DisplayUtils displayStringByDecimalNumber:item.price];
    _commentLabel.text = item.comment;
    _nameLabel.text = item.name;
    self.image = [UIImage imageNamed:@"yakitori1.jpg"];
    _item = item;
}

- (void)setImage:(UIImage *)image
{
    CGFloat mag = 280/image.size.height;
    CGFloat height = image.size.height*mag;
    CGFloat width = image.size.width*mag;
    
//    NSLog(@"-->%f", height);
//    NSLog(@">>>%f", width);
    
    //[_scrollview setContentSize:CGSizeMake(width, height)];
    _scrollview.contentSize = CGSizeMake(width, height);
    _imageView.frame = CGRectMake(0, 0, width, height);
    
    _image = image;
    _imageView.image = image;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.frame = CGRectMake(self.frame.origin.x+3, self.frame.origin.y+3,
                            self.frame.size.width, self.frame.size.height);
    
    if ([self.delegate respondsToSelector:@selector(itemThumbnailViewTouchBegan:)]) {
        [self.delegate itemThumbnailViewTouchBegan:self];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.frame = _myFrame;
    
    if ([self.delegate respondsToSelector:@selector(itemThumbnailViewTouchEnded:)]) {
        [self.delegate itemThumbnailViewTouchEnded:self];
    }
    
    CGPoint point = [[touches anyObject] locationInView:self];
    if (0 < point.x && point.x <= self.frame.size.width &&
        0 < point.y && point.y <= self.frame.size.height) {
        if ([self.delegate respondsToSelector:@selector(itemThumbnailViewDidTouchUpInside:)]) {
            [self.delegate itemThumbnailViewDidTouchUpInside:self];
        }
    }
}
@end
